//
//  DWECollectionView.m
//  休闲吧
//
//  Created by DWE_KJ on 2017/1/5.
//  Copyright © 2017年 loveMe. All rights reserved.
//

#import "DWECollectionView.h"
#import "DWECollectionViewCell.h"
//#import "DWEHomeModel.h"
//#import "DWEDetailsInfoController.h"
//#import "UIColor+Util.h"
#import "Masonry.h"

@interface DWECollectionView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UICollectionViewFlowLayout *layout;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *locationdesLabel;
@property (nonatomic, assign) CGPoint point;

@end

@implementation DWECollectionView

+ (instancetype)initWithCollectionViewPoint:(CGPoint)point
{
    return [[self alloc]initWithPoint:point];
}

- (instancetype)initWithPoint:(CGPoint)point
{
    if (self = [super initWithFrame:[UIScreen mainScreen].bounds])
    {
        _contentView = [[UIView alloc]init];
        _contentView.backgroundColor = [UIColor clearColor];
        _contentView.layer.cornerRadius = 5;
        _contentView.layer.masksToBounds = YES;
        
        _headerView = [UIView new];
//        _headerView.backgroundColor = [UIColor colorWithHex:0X3bb4ec];
        _headerView.backgroundColor = [UIColor blackColor];
        [_contentView addSubview:_headerView];
        
        _nameLabel = [UILabel new];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = [UIFont systemFontOfSize:13];
        [_headerView addSubview:_nameLabel];
        
        _locationdesLabel = [UILabel new];
        _locationdesLabel.numberOfLines = 0;
        _locationdesLabel.textColor = [UIColor whiteColor];
        _locationdesLabel.font = [UIFont systemFontOfSize:13];
        [_headerView addSubview:_locationdesLabel];
        
        _layout = [[UICollectionViewFlowLayout alloc]init];
        _layout.minimumInteritemSpacing = 5;
        _layout.minimumLineSpacing = 10;
        _layout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
        [_collectionView registerClass:[DWECollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
        [_collectionView registerClass:[DWENoCollectionViewCell class] forCellWithReuseIdentifier:@"NoCell"];
        _collectionView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.5];
        [self.contentView addSubview:_collectionView];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(5);
            make.left.equalTo(_locationdesLabel.mas_left);
            make.top.mas_equalTo(2);
        }];
        [_locationdesLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_nameLabel.mas_bottom).offset(0);
            make.right.mas_lessThanOrEqualTo(-2);
        }];
    }
    return self;
}

- (void)setHeaderViewName:(NSString *)name locationdes:(NSString *)locationdes
{
    _nameLabel.text = [NSString stringWithFormat:@"店名:%@",name];
    _locationdesLabel.text = [NSString stringWithFormat:@"地址:%@",locationdes];
}

- (void)willMoveToWindow:(UIWindow *)newWindow
{
    [self.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    [self addSubview:_contentView];
    
    NSUInteger count = self.dataArray.count;
    CGFloat sWidth = CGRectGetWidth([UIScreen mainScreen].bounds);
    CGFloat sHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
    CGFloat height = 230;
    CGFloat width = 240;
    if (count <= 4)
    {
        height = 110;
    }
    else if (count > 4 && count <= 8)
    {
        height = 170;
    }
    self.headerView.frame = CGRectMake(0, 0, width, 50);
    self.contentView.bounds = CGRectMake(0, 0, width, height);
    self.contentView.center = CGPointMake(sWidth *0.5, sHeight*0.5);
    self.collectionView.frame = CGRectMake(0, 50, width, height-50);
    [self.collectionView reloadData];
}

- (void)show
{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    if (window)
    {
        window = [UIApplication sharedApplication].windows[0];
    }
    [window addSubview:self];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    if ([touch.view isEqual:self])
    {
        [self removeFromSuperview];
    }
}

#pragma mark -- UICollectionViewDelegate UICollectionViewDelegateFlowLayout
#pragma mark - UICollectionView

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count == 0 ? 1 : self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataArray.count == 0)
    {
        DWENoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"NoCell" forIndexPath:indexPath];
        cell.promptLable.text = @"暂无注册技师";
        return cell;
    }
    else
    {
        DWECollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
//        if (indexPath.item < self.dataArray.count)
//        {
//            DWEHomeModel *model = self.dataArray[indexPath.item];
//            [cell setUIModel:model];
//        }
        return cell;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width = 50;
    if (self.dataArray.count == 0)
    {
        width = 150;
    }
    return CGSizeMake(width, 50);
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    NSLog(@"%ld",(long)indexPath.item);
//    if (indexPath.item < self.dataArray.count)
//    {
//        DWEHomeModel *model = self.dataArray[indexPath.item];
//        [self removeFromSuperview];
//        [self popDetailsInfoVC:model];
//    }
}

//- (void)popDetailsInfoVC:(DWEHomeModel *)model
//{
//    if (model.jsId)
//    {
//        UIStoryboard *jsStoryBoard = [UIStoryboard storyboardWithName:@"DetailsInfo" bundle:[NSBundle mainBundle]];
//        DWEDetailsInfoController *detailsInfoVC = [jsStoryBoard instantiateViewControllerWithIdentifier:@"detailsInfo"];
//        detailsInfoVC.jsId = model.jsId;
//        UIViewController *vc = [Utils getCurrentViewController];
//        [vc.navigationController pushViewController:detailsInfoVC animated:YES];
//    }
//}

@end
